package com.yuexun.dhdoctor.module.Contract.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.module.Contract.bean.People;

import java.util.ArrayList;

/**
 * Created by work-jx on 2016/7/18.
 */
public class ContractAdapter extends BaseAdapter {
    private ArrayList<People> coll;
    private ArrayList<People> mOriginalValues;
    protected LayoutInflater mInflater;
    private int type;
    private Context context;
    //    private AsyncImageLoader imageLoader;
    private ListView listView;

    public ContractAdapter(Context context, ArrayList<People> coll, int type) {
        this.coll = coll;
        this.context = context;
        this.type = type;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public ContractAdapter(Context context, ArrayList<People> coll, int type, ListView listView) {
        this.listView = listView;
        this.coll = coll;
        this.context = context;
        this.type = type;
//        imageLoader = new AsyncImageLoader();
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(ArrayList<People> data) {
        this.coll = data;
        notifyDataSetChanged();
    }

    public int getSortKeyPosition(int first) {
        int position = -1;
        if (coll != null) {
            for (int i = 0; i < coll.size(); i++) {
                Log.e("jx", coll.get(i).getPinyin().charAt(0) + "");
                if (!"".equals(coll.get(i).getPinyin()) && coll.get(i).getPinyin().charAt(0) == first) {
                    position = i;
                    break;
                }
            }
        }
        return position;
    }

    public int getCount() {
        if (coll == null) {
            return 0;
        }
        return coll.size();
    }

    public Object getItem(int position) {
        if (position < 0 || position >= coll.size())
            return null;
        return coll.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final viewHolder1 holder1;
        if (convertView == null) {
            convertView = this.mInflater.inflate(R.layout.grouplistitem, parent, false);
            holder1 = new viewHolder1();
            holder1.pic = (ImageView) convertView.findViewById(R.id.ivPhoto);
//            holder1.roundpic = (CircularImage) convertView.findViewById(R.id.roundImg);
            holder1.name = (TextView) convertView.findViewById(R.id.tvName);
            holder1.jobNum = (TextView) convertView.findViewById(R.id.tvJobNum);
            holder1.text1 = (TextView) convertView.findViewById(R.id.tvCount);
            holder1.text2 = (TextView) convertView.findViewById(R.id.tvPosition);
//            holder1.btn = (MyImageButton) convertView.findViewById(R.id.btnChat);
            convertView.setTag(holder1);
        } else {
            holder1 = (viewHolder1) convertView.getTag();
        }

        holder1.name.setText(coll.get(position).getUserName());
        holder1.jobNum.setText("工号" + coll.get(position).getJobNum());
        holder1.pic.setImageResource(R.drawable.pic_girl);
        holder1.pic.setVisibility(View.VISIBLE);
//        holder1.roundpic.setVisibility(View.GONE);

        if (coll.get(position).getDepartmentName() != null) {
            holder1.text1.setText(coll.get(position).getDepartmentName());
        } else
            holder1.text1.setText("");

        if (coll.get(position).getPosition() != null) {
            holder1.text2.setText(coll.get(position).getPosition());
        } else {
            holder1.text2.setText("");
        }

        return convertView;
    }

    class viewHolder1 {
        ImageView pic;
//        CircularImage roundpic;
        TextView jobNum;
        TextView name;
        TextView text1;
        TextView text2;
//        MyImageButton btn;
    }

    public void getMyFilter(String s) {
        if (mOriginalValues == null) {
            mOriginalValues = new ArrayList<People>(coll);
        }
        ArrayList<People> list = new ArrayList<People>();
        for (int i = 0; i < mOriginalValues.size(); i++) {
            People data = mOriginalValues.get(i);
            if ((data.getUserName() != null && data.getUserName().contains(s))
                    || (data.getPinyin() != null && data.getPinyin().contains(s.toUpperCase()))
                    || String.valueOf(data.getUserId()).contains(s)
                    || (data.getPhone() != null && data.getPhone().contains(s))
                    || (data.getPhone() != null && data.getPhone().contains(s))
                    ) {
                list.add(data);
            }
        }

        coll = list;
        notifyDataSetChanged();

    }
}
